package cn.hznc.controller.Message;

import cn.hznc.controller.service.message.MessageService;
import cn.hznc.domain.entity.MessageEntity;
import cn.hznc.domain.request.life.QueryToolLifeReq;
import cn.hznc.domain.request.message.GetMessageReq;
import cn.hznc.domain.request.message.SelectUserByConditionReq;
import cn.hznc.domain.request.message.SendMessageReq;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.ToolManageException;
import cn.hznc.exception.UserException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/10/23 14:18
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/toolLife")
@Slf4j
public class MessageController{

    @Autowired
    private MessageService messageService;

    /**
     * 工单下达消息存储
     *
     * @return
     */
    @RequestMapping(value = "/releasedOrderMessage", method = RequestMethod.GET)
    public ResponseBody releasedOrderMessage(@RequestParam("conditions") List<Integer> conditions) {
        return ResponseBody.success(messageService.releasedOrderMessage(conditions));
    }


    /**
     * 查询程序消息推送
     *
     * @return
     */
    @RequestMapping(value = "/programMessage", method = RequestMethod.GET)
    public ResponseBody programMessage(@RequestParam("taskId") String taskId) {
        return ResponseBody.success(messageService.programMessage(taskId));
    }

    /**
     * 查询取刀消息推送
     *
     * @return
     */
    @RequestMapping(value = "/getToolMessage", method = RequestMethod.GET)
    public ResponseBody getToolMessage(@RequestParam("id")Integer id) {
        return ResponseBody.success(messageService.getToolMessage(id));
    }

    /**
     * 查询为满足刀具消息推送
     *
     * @return
     */
    @RequestMapping(value = "/unmetToolListMessage", method = RequestMethod.GET)
    public ResponseBody unmetToolListMessage() {
        return ResponseBody.success(messageService.unmetToolListMessage());
    }

    /**
     * 查看消息
     *
     * @return
     */
    @RequestMapping(value = "/getMessageList", method = RequestMethod.POST)
    public ResponseBody getMessageList(@RequestBody GetMessageReq getMessageReq) {
        return ResponseBody.success(messageService.getMessageList(getMessageReq));
    }

    @RequestMapping(value = "/editMessageStatus", method = RequestMethod.POST)
    public ResponseBody editMessageStatus(@RequestBody MessageEntity messageEntity) {
        return ResponseBody.success(messageService.editMessageStatus(messageEntity));
    }

    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
    public ResponseBody sendMessage(@RequestBody SendMessageReq sendMessageReq) {
        try {
            return ResponseBody.success(messageService.sendMessage(sendMessageReq));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode() , e.getMessage());
        }
    }

    /**
     * 根据该用户id以及消息类型查看可接收消息的用户
     * @return
     */
    @RequestMapping(value = "/selectUserByCondition", method = RequestMethod.POST)
    public ResponseBody selectUserByCondition(@RequestBody SelectUserByConditionReq selectUserByConditionReq) {
        return ResponseBody.success(messageService.selectUserByCondition(selectUserByConditionReq));
    }

    /**
     * 根据该用户id查询可使用的消息类型
     * @return
     */
    @RequestMapping(value = "qryMessageTypeByUserId", method = RequestMethod.GET)
    public ResponseBody qryMessageTypeByUserId(@RequestParam("userId") Integer userId) {
            try {
                return ResponseBody.success(messageService.qryMessageTypeByUserId(userId));
            } catch (UserException e) {
                return ResponseBody.error(e.getCode() ,e.getMessage());
            }
    }

}
